<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class DebateController extends JController
{
	function display()
		{	
			parent :: display();
		}
	function approve(){
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		// JRequest::setVar( 'view', 'approve' );
		// JRequest::setVar( 'layout', 'default'  );     
		// JRequest::setVar( 'user_id', $cid   );     
		$this->setRedirect(JRoute::_('index.php?option=com_debate&view=approve&user_id='.$cid[0], false));
		parent::display();
	}	
	function pending(){
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		// JRequest::setVar( 'view', 'penddingapprove' );
		// JRequest::setVar( 'layout', 'default'  );    
		$this->setRedirect(JRoute::_('index.php?option=com_debate&view=pending&user_id='.$cid[0], false));				
		parent::display();
	}	
	function rejected(){
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		// JRequest::setVar( 'view', 'rejected' );
		// JRequest::setVar( 'layout', 'default'  );   
		$this->setRedirect(JRoute::_('index.php?option=com_debate&view=rejected&user_id='.$cid[0], false));		
		parent::display();
	}	
	function save(){
		$userid = JRequest::getVar( 'user_id' );
		$checkeds = JRequest::getVar( 'cid', array(0), '', 'array' );
		$view = JRequest::getVar( 'view' );
		$db =& JFactory::getDBO();
		$query = "SELECT user_id FROM #__debate WHERE user_id = ".$userid.";";
		$db->setQuery($query);
		if(!$db->loadObjectList()){
			if($view == 'approve')
			$query = "INSERT INTO #__debate ( user_id, approve_debate )
											VALUES
											( ".$userid.", '".serialize($checkeds)."' );";
			if($view == 'pending')
			$query = "INSERT INTO #__debate ( user_id, pending_debate )
											VALUES
											( ".$userid.", '".serialize($checkeds)."' );";
			if($view == 'rejected'){
			$query = "INSERT INTO #__debate ( user_id, rejected_debate )
											VALUES
											( ".$userid.", '".serialize($checkeds)."' );";								
			}
		} 
		else{
		
			if($view == 'approve')
			$query = "UPDATE #__debate SET approve_debate = '".serialize($checkeds)."' WHERE user_id = ".$userid.";";
			if($view == 'pending')
			$query = "UPDATE #__debate SET pending_debate = '".serialize($checkeds)."' WHERE user_id = ".$userid.";";
			if($view == 'rejected'){
				$query = "UPDATE #__debate SET rejected_debate = '".serialize($checkeds)."' WHERE user_id = ".$userid.";";
				$appquery = "SELECT approve_debate FROM #__debate WHERE user_id = ".$userid.";";
				$db->setQuery($appquery);
				$app = $db->loadObjectList();
				if($app) 
				{
					$app = unserialize($app[0]->approve_debate);
					$app = array_diff($app, $checkeds);
					$appquery = "UPDATE #__debate SET approve_debate = '".serialize($app)."' WHERE user_id = ".$userid.";";
					$db->setQuery($query);
					$db->query();
				}
				$penquery = "SELECT pending_debate FROM #__debate WHERE user_id = ".$userid.";";
				$db->setQuery($penquery);
				$pen = $db->loadObjectList();
				if($pen)
				{ 	
					$pen = unserialize($pen[0]->pending_debate);
					$pen = array_diff($pen, $checkeds);
					$penquery = "UPDATE #__debate SET pending_debate = '".serialize($pen)."' WHERE user_id = ".$userid.";";
					$db->setQuery($query);
					$db->query();
				}
			}
		}
		$db->setQuery($query);
		$db->query();
		parent::display();
	}	
}

